package com.coffee.order.service.sku.impl;

import com.coffee.core.entity.Page;
import com.coffee.core.entity.PagerInfo;
import com.coffee.core.enums.ExceptionEnum;
import com.coffee.core.exception.BusinessException;
import com.coffee.core.util.BeanUtilExt;
import com.coffee.core.util.DTOUtils;
import com.coffee.core.util.StringUtil;
import com.coffee.order.api.sku.query.EnjoyBeforePaySkuQuery;
import com.coffee.order.api.sku.result.EnjoyBeforePaySkuResult;
import com.coffee.order.api.sku.service.EnjoyBeforePaySkuService;
import com.coffee.order.service.sku.bean.EnjoyBeforePaySku;
import com.coffee.order.service.sku.dao.EnjoyBeforePaySkuDao;
import com.coffee.sku.api.sku.query.SkuSalesInformationQuery;
import com.coffee.sku.api.sku.req.SkuListReq;
import com.coffee.sku.api.sku.result.SkuListResult;
import com.coffee.sku.api.sku.result.SkuSalesInformationResult;
import com.coffee.sku.service.sku.bean.SkuCategory;
import com.coffee.sku.service.sku.bean.SkuSalesInformation;
import com.coffee.sku.service.sku.dao.SkuCategoryDao;
import com.coffee.sku.service.sku.dao.SkuSalesInformationDao;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;

import javax.annotation.Resource;
import java.util.*;


@DubboService(dynamic = true)
public class EnjoyBeforePaySkuServiceImpl implements EnjoyBeforePaySkuService {
    @Resource
    private EnjoyBeforePaySkuDao enjoyBeforePaySkuDao;
    @Resource
    private SkuCategoryDao skuCategoryDao;
    @Resource
    private SkuSalesInformationDao skuSalesInformationDao;

    @Override
    public Page<SkuListResult> getPager(SkuListReq param, PagerInfo pr) {
        Page<SkuListResult> pager = new Page<SkuListResult>(pr.getStart(), pr.getPageSize());
        Integer count = enjoyBeforePaySkuDao.count(param);
        pager.setTotalPage(count);
        if (count > 0) {
            List<SkuListResult> page = enjoyBeforePaySkuDao.page(param, pr.getStart(), pr.getPageSize());
            for (SkuListResult skuListRep : page) {
                if (StringUtils.isNotEmpty(skuListRep.getPidPath())) {
                    String[] split = skuListRep.getPidPath().split(",");
                    SkuCategory skuCategory = skuCategoryDao.findById(Integer.parseInt(split[0]));
                    if (skuCategory != null) {
                        skuListRep.setCategoryName(skuCategory.getName());
                    }
                }
                SkuSalesInformationQuery skuSalesInformationQuery = new SkuSalesInformationQuery();
                skuSalesInformationQuery.setSkuId(skuListRep.getSkuId());
                List<SkuSalesInformation> skuSalesInformations = skuSalesInformationDao.list(skuSalesInformationQuery);
                skuListRep.setSkuSalesInformationResults(DTOUtils.convertList(skuSalesInformations, SkuSalesInformationResult.class));
            }
            pager.setData(page);
        }
        return pager;
    }


    @Override
    public EnjoyBeforePaySkuResult getDetail(Integer id) {
        EnjoyBeforePaySku item = enjoyBeforePaySkuDao.findById(id);
        if (item == null) {
            return null;
        }
        EnjoyBeforePaySkuResult result = new EnjoyBeforePaySkuResult();
        BeanUtilExt.copyProperties(result, item);

        return result;
    }


    @Override
    public void delete(Integer id) {
        enjoyBeforePaySkuDao.deleteById(id);
    }

    @Override
    public void deleteBySkuId(Integer id) {
        enjoyBeforePaySkuDao.deleteBySkuId(id);
    }

    @Override
    public void addOrUpdate(EnjoyBeforePaySkuResult result) throws BusinessException {
        EnjoyBeforePaySku item = new EnjoyBeforePaySku();
        BeanUtilExt.copyProperties(item, result);
        if (item.getId() == null) {
            enjoyBeforePaySkuDao.insert(item);
        } else {
            EnjoyBeforePaySku tmp = enjoyBeforePaySkuDao.findById(item.getId());
            if (tmp == null) {
                throw new BusinessException(ExceptionEnum.ERROR_0002.getCode(), ExceptionEnum.ERROR_0002.getMessage());
            }
            enjoyBeforePaySkuDao.updateById(item);
        }
    }

    @Override
    public void addBySkuIds(List<Integer> skuIds) throws BusinessException {
        List<Integer> existenceSkuIds = enjoyBeforePaySkuDao.findSkuIds();
        Collection subtract = CollectionUtils.subtract(skuIds, existenceSkuIds);
        List<Integer> addList = new ArrayList<>(subtract);
        if (addList.size() > 0) {
            List<EnjoyBeforePaySku> list = new ArrayList<>();
            for (Integer integer : addList) {
                EnjoyBeforePaySku param = new EnjoyBeforePaySku();
                param.setSkuId(integer);
                list.add(param);
            }
            enjoyBeforePaySkuDao.insertBatch(list);
        }
    }


    @Override
    public Map<Integer, EnjoyBeforePaySkuResult> getItemMap(Set<Integer> idSet) {
        Map<Integer, EnjoyBeforePaySkuResult> map = new HashMap<Integer, EnjoyBeforePaySkuResult>();
        List<EnjoyBeforePaySku> list = enjoyBeforePaySkuDao.findByIds(StringUtil.setToList(idSet));
        List<EnjoyBeforePaySkuResult> resultList = DTOUtils.convertList(list, EnjoyBeforePaySkuResult.class);
        for (EnjoyBeforePaySkuResult item : resultList) {
            map.put(item.getId(), item);
        }
        return map;
    }


}
